<?php
/**
 * 
 *
 * @category 
 * @package 
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class erpapi_shop_matrix_taobao_response_order extends erpapi_shop_response_order
{
    /**
     * 订单obj明细唯一标识
     *
     * @var string
     **/
    public $object_comp_key = 'bn-oid-obj_type';

    /**
     * 订单item唯一标识
     *
     * @var string
     **/
    public $item_comp_key = 'bn-shop_product_id-item_type';


    public function business_flow($sdf)
    {
        if ($sdf['t_type'] == 'fenxiao' || $sdf['order_source'] == 'taofenxiao') {
            $order_type = 'b2b';
        } else {
            $order_type = 'b2c';
        }

        return kernel::single('erpapi_shop_matrix_taobao_response_order_'.$order_type);
    }


    protected function _operationSel()
    {
        parent::_operationSel();

        $lastmodify = kernel::single('ome_func')->date2time($this->_ordersdf['lastmodify']);

        // 解决订单备注没更新(淘宝平台问题，备注修改订单最后时间不变)
        if (!$this->_operationSel && $lastmodify == $this->_tgOrder['outer_lastmodify']) $this->_operationSel = 'update';

    }

    public function _canAccept()
    {
        if($this->__channelObj->channel['business_type']=='zx' && in_array($this->_ordersdf['order_source'],array('tbdx','tbjx'))) {
            $this->__apilog['result']['msg'] = '直销店铺不接收分销订单';
            return false;
        }

        if($this->__channelObj->channel['business_type']=='fx' && !in_array($this->_ordersdf['order_source'],array('tbdx','tbjx'))) {
            $this->__apilog['result']['msg'] = '分销店铺不接收直销订单';
            return false;
        }

        foreach($this->_ordersdf['order_objects'] as $object){
            if (in_array($object['zhengji_status'],array('1','3'))){
                $this->__apilog['result']['msg'] = '征集中和征集失败订单不收!';
                return false;
            }

            if(!empty($object['is_sh_ship'])){
                if($object['is_sh_ship'] == 'true'){
                    $this->__apilog['result']['msg'] = '菜鸟自动流转订单,不接受';
                    return false;
                }
            }
        }

        if(in_array($this->_ordersdf['step_trade_status'],array('FRONT_NOPAID_FINAL_NOPAID','FRONT_PAID_FINAL_NOPAID'))){
            $this->__apilog['result']['msg'] = '定金未付尾款未付或定金已付尾款未付订单不接收';
            return false;
        }

        if($this->_ordersdf['other_list']){
            foreach((array) $this->_ordersdf['other_list'] as $val){
                // 淘宝处方类订单
                if($val['type'] == 'rx_audit' && $val['rx_audit_status'] == '0'){
                    $this->__apilog['result']['msg'] = '处方药未审核状态，不接受';
                    return false;
                }
            }
        }

        return parent::_canAccept();
    }

    protected function get_create_plugins()
    {
        $plugins = parent::get_create_plugins();

        $plugins[] = 'tbgift';
        // $plugins[] = 'tbjz';
        $plugins[] = 'tboid';//[拆单]保存淘宝平台的原始属性值

        return $plugins;
    }

    protected function get_update_components()
    {
        $components = array('markmemo','custommemo','marktype','oversold');

        if(in_array($this->_tgOrder['process_status'], array('unconfirmed'))){
            $rs = app::get('ome')->model('order_extend')->getList('extend_status',array('order_id'=>$this->_tgOrder['order_id']));
            // 如果ERP收货人信息未发生变动时，则更新淘宝收货人信息
            if ($rs[0]['extend_status'] != 'consignee_modified') {
                $components[] = 'consignee';
            }
        }

        return $components;
    }

    protected function get_convert_components()
    {
        $components = parent::get_convert_components();
        $components[] = 'oversold';
        return $components;
    }
}
